﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.IO.Compression;
using log4net;

/// <summary>
/// Summary description for AfitFile
/// </summary>
public class AfitFile
{
    private static readonly ILog _logger = LogManager.GetLogger(typeof(AfitFile).Name);
    public AfitFile()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static void ZipCompressFile(string source, string zipFile)
    {
        using (FileStream fs = new FileStream(zipFile, FileMode.Create, FileAccess.Write))
        using (GZipStream gzs = new GZipStream(fs, CompressionMode.Compress))
        {
            byte[] data = File.ReadAllBytes(source);
            gzs.Write(data, 0, data.Length);
            gzs.Flush();
        }
    }

    public static void ZipDecompressFile(string zipFile, string originalFile)
    {
        using (FileStream fsZip = new FileStream(zipFile, FileMode.Open, FileAccess.Read))
        using (FileStream fsOriginal = new FileStream(originalFile, FileMode.Create, FileAccess.Write))
        using (GZipStream gzs = new GZipStream(fsZip, CompressionMode.Decompress))
        {
            while (true)
            {
                byte[] buffer = new byte[100];
                int bytesRead = gzs.Read(buffer, 0, buffer.Length);
                fsOriginal.Write(buffer, 0, bytesRead);
                if (bytesRead != buffer.Length)
                {
                    break;
                }
            }
        }
    }
    public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
    {
        bool ret = false;
        try
        {
            SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
            DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

            if (Directory.Exists(SourcePath))
            {
                if (Directory.Exists(DestinationPath) == false)
                    Directory.CreateDirectory(DestinationPath);

                foreach (string fls in Directory.GetFiles(SourcePath))
                {
                    FileInfo flinfo = new FileInfo(fls);
                    if(!IsFileLocked(flinfo,DestinationPath))
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                }
                foreach (string drs in Directory.GetDirectories(SourcePath))
                {
                    DirectoryInfo drinfo = new DirectoryInfo(drs);
                    if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                        ret = false;
                }
            }
            ret = true;
        }
        catch (Exception ex)
        {
            ret = false;
            _logger.Error(ex.Message);
        }
        return ret;
    }
    public static bool DeleteFolder(string path)
    {
        try
        {
            System.IO.DirectoryInfo ptsFolder = new DirectoryInfo(path);
            if (ptsFolder.Exists)
            {
                foreach (FileInfo file in ptsFolder.GetFiles())
                {
                    file.Delete();
                }
                foreach (DirectoryInfo dir in ptsFolder.GetDirectories())
                {
                    dir.Delete(true);
                }
            }
            if (Directory.Exists(path))
                Directory.Delete(path);
            return true;
        }
        catch (Exception ex)
        {
            _logger.Error(ex.Message);
            return false;
        }
    }
    public static bool DeleteFile(string path)
    {
        try
        {
            if (File.Exists(path))
                File.Delete(path);
            return true;
        }
        catch (Exception ex)
        {
            _logger.Error(ex.Message);
            return false;
        }
    }
    public static bool IsFileLocked(FileInfo file, string DestinationPath)
    {
        FileStream stream = null;

        try
        {
            file.CopyTo(DestinationPath + file.Name, true);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
        return false;
    }
}
